package cn.panda.edu.controller;

import cn.panda.edu.model.Student;
import cn.panda.edu.service.StudentService;
import cn.panda.edu.util.Constant;
import cn.panda.edu.util.FileUtil;
import cn.panda.edu.util.MD5;
import cn.panda.edu.util.ResultDataUtil;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping(value = "/api/admin")
@Api(description="学生管理")
public class ManageStudentController {
	
	@Autowired
	private StudentService studentService;
	
	/**
	 * 
	* @Title: StudentmanageFy 
	* @Description: TODO(分页+根据学生编号或学生姓名模糊查询学生列表 排序) 
	* @param studentId
	* @param studentName
	* @param pageIndex
	* @return  
	* PageInfo<Student>
	 */
	@ApiOperation(value="获取学生列表   分页 排序" , notes="分页+模糊查询学生列表")//用以指明api名称 
	@ApiImplicitParams({
		@ApiImplicitParam(name = "student", value ="学生类", required = false, dataType = "Student", paramType = "form"),
		@ApiImplicitParam(name = "pageIndex", value ="当前页码", required =true, dataType = "int", paramType="form")
	})
	@RequestMapping(value = "/StudentmanageFy", method = RequestMethod.POST)
	public Object StudentmanageFy(Student student,int pageIndex){
	
		PageInfo<Student> pList = studentService.selectByPageAndSelective(student, pageIndex, Constant.PAGE_SIZE);
		return ResultDataUtil.resData(200, "", pList);
	}
	
	/**
	 * 
	* @Title: Studentmanageadd 
	* @Description: TODO(管理添加学生信息) 
	* @param student
	* @return  
	* Map<String,Object>
	 */
	@ApiOperation(value = "添加学生信息" , notes="")
	@ApiImplicitParam(name = "student", value = "学生实体类Student", required = true, dataType = "Student", paramType="form")
	@RequestMapping(value = "/StudentmanageAdd",method = RequestMethod.POST)
	public Map<String, Object> StudentmanageAdd(Student student) {
		student.setStudentDate(new Date());
		
		student.setStudentPassword(MD5.encryptPassword("123456",student.getStudentName()));
		Map<String, Object> map = new HashMap<>();
		int result = studentService.insertSelective(student);
		if(result > 0) {
			map = ResultDataUtil.resData(200, "添加成功",null) ;
		} else {
			map = ResultDataUtil.resData(200, "添加失败",null) ;
		}
		return map;
	}
	
	/**
	 * 
	* @Title: StudentmanageXq 
	* @Description: TODO(查询学生信息详情) 
	* @param studentId
	* @return  
	* Map<String,Object>
	 */
	@ApiOperation(value = "查询学生信息详情" , notes="")
	@ApiImplicitParam(name = "studentId", value = "学生编号", required = true, dataType = "int", paramType="form")
	@RequestMapping(value = "/StudentmanageSel",method = RequestMethod.POST)
	public Map<String, Object> StudentmanageXq(Integer studentId) {
		Student student = studentService.selectByPrimaryKey(studentId);
		return ResultDataUtil.resData(200, "", student);
		
	}
	
	/**
	 * 
	* @Title: StudentmanageUp 
	* @Description: TODO(管理修改学生信息) 
	* @param student
	* @return  
	* Map<String,Object>
	 */
	@ApiOperation(value = "修改学生信息", notes="")
	@ApiImplicitParam(name = "student", value = "学生实体类Student", required = false, dataType = "Student",paramType="form")
	@RequestMapping(value = "/StudentmanageUp",method = RequestMethod.POST)
	public Map<String, Object> StudentmanageUp(Student student){
		Map<String, Object> map = new HashMap<>();
		student.setStudentModifyDate(new Date());
		int result = studentService.updateByPrimaryKeySelective(student);
		if(result > 0) {
			map = ResultDataUtil.resData(200, "修改成功",null) ;
		} else {
			map = ResultDataUtil.resData(500, "修改失败",null) ;
		}
		return map;
		
	}
	
	/**
	 * 
	* @Title: StudentmanageDel 
	* @Description: TODO(管理删除学生信息) 
	* @param studentId
	* @return  
	* Map<String,Object>
	 */
	@ApiOperation(value = "管理删除学生信息", notes ="")
	@ApiImplicitParam(name = "studentId", value ="学生编号", required = true, dataType = "int", paramType = "form")
	@RequestMapping(value = "/StudentmanageDel", method = RequestMethod.POST)
	public Map<String, Object> StudentmanageDel(Integer studentId){
		Map<String, Object> map = new HashMap<>();
		int result = studentService.deleteByPrimaryKey(studentId);
		if(result > 0) {
			map = ResultDataUtil.resData(200, "删除成功",null) ;
		} else {
			map = ResultDataUtil.resData(500, "删除成功",null) ;
		}
		return map;
		
	}
	/**
	 * 
	* @Title: uploadStuPhoto 
	* @Description: TODO(上传学生照片) 
	* @param @param file
	* @param @param request
	* @param @return  
	* @return Object
	 */
	@ApiOperation(value="上传学生照片")
	@RequestMapping(value="/uploadStuPhoto",method=RequestMethod.POST)
	public  Object uploadStuPhoto(@RequestParam("file") MultipartFile file,
            HttpServletRequest request) {
		
        String filePath = null ;
        try {
            filePath = FileUtil.uploadFile(file) ;
        } catch (Exception e) {
            e.printStackTrace();
        }
        
		return ResultDataUtil.resData(200, "上传成功！", filePath);
	}

}
